package com.rnd.kx.mvp.ui;

import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rnd.kx.mvp.R;
import com.rnd.kx.mvp.adapter.GGAdapter;
import com.rnd.kx.mvp.adapter.PopAdapter;
import com.rnd.kx.mvp.bean.Product;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Main3Activity extends AppCompatActivity implements View.OnClickListener {

    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    @BindView(R.id.layout_add)
    RelativeLayout layout_add;
    private ArrayList<String> list;
    private GGAdapter ggAdapter;
    private PopupWindow popupWindow;
    private ArrayList<Product> products;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        ButterKnife.bind(this);

        list = new ArrayList<>();
        products = new ArrayList<>();
        list.add("");
        final Product product = new Product();
        product.setIs_buy_new("0");
        product.setIs_change_new("0");
        product.setIs_annual_inspection("0");
        products.add(product);
        ggAdapter = new GGAdapter(this, products);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(ggAdapter);

        ggAdapter.setOnItemClickListener(new GGAdapter.OnItemClickListener() {
            @Override
            public void onClick(View view, GGAdapter.ViewName viewName, int position, int[] location) {
                RelativeLayout layout_type = (RelativeLayout) view.findViewById(R.id.layout_type);
                RelativeLayout layout_size = (RelativeLayout) view.findViewById(R.id.layout_size);
                TextView num = (TextView) view.findViewById(R.id.num);
                CheckBox checkBox1 = (CheckBox) view.findViewById(R.id.chebox1);
                CheckBox checkBox2 = (CheckBox) view.findViewById(R.id.chebox2);
                CheckBox checkBox3 = (CheckBox) view.findViewById(R.id.checkbox3);
                Integer intNum ;
                switch (viewName){
                    case LAYOUT_TYPE:
                        showPop(layout_type,"1",location,position);
                        break;
                    case LAYOUT_SIZE:
                        showPop(layout_size,"2",location,position);
                        break;
                    case IV_SUB:
                        if (products.get(position).getStatus() != null && !products.get(position).getStatus().equals("0")){
                            intNum = Integer.valueOf(products.get(position).getStatus());
                            intNum = intNum - 1;
                            products.get(position).setStatus(intNum+"");
                            ggAdapter.notifyDataSetChanged();
                        }
                        break;
                    case IV_ADD:
                        if (products.get(position).getStatus() != null){
                            intNum = Integer.valueOf(products.get(position).getStatus());
                        }else {
                            intNum = 0;
                        }
                        intNum = intNum + 1;
                        products.get(position).setStatus(intNum+"");
                        ggAdapter.updateData(products);
                        break;
                    case CHECKBOX1:
                        if (product.getIs_buy_new().equals("1")){
                            products.get(position).setIs_buy_new("0");
                        }else {
                            products.get(position).setIs_buy_new("1");
                        }
                        if (product.getIs_change_new().equals("1")){
                            products.get(position).setIs_change_new("0");
                        }
                        if (products.get(position).getIs_annual_inspection().equals("1")){
                            products.get(position).setIs_annual_inspection("0");
                        }
                        ggAdapter.notifyDataSetChanged();
                        break;
                    case CHECKBOX2:
                        if (product.getIs_buy_new().equals("1")){
                            products.get(position).setIs_buy_new("0");
                        }
                        if (product.getIs_change_new().equals("1")){
                            products.get(position).setIs_change_new("0");
                        }else {
                            products.get(position).setIs_change_new("1");
                        }
                        if (product.getIs_annual_inspection().equals("1")){
                            products.get(position).setIs_annual_inspection("0");
                        }

                        ggAdapter.notifyDataSetChanged();
                        break;
                    case CHECKBOX3:
                        if (product.getIs_buy_new().equals("1")){
                            products.get(position).setIs_buy_new("0");
                        }
                        if (product.getIs_change_new().equals("1")){
                            products.get(position).setIs_change_new("0");
                        }
                        if (product.getIs_annual_inspection().equals("1")){
                            products.get(position).setIs_annual_inspection("0");
                        }else {
                            products.get(position).setIs_annual_inspection("1");
                        }


                        ggAdapter.notifyDataSetChanged();
                        break;

                }
            }
        });

        layout_add.setOnClickListener(this);
    }

    private void showPop(View view, final String s, int[] location, final int i) {

        Log.e("csdLocation",location[0]+"---"+location[1]);

        if (popupWindow != null && popupWindow.isShowing()){
            return;
        }
        LinearLayout layout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.layout_popupwindow, null);
        RecyclerView popRV = (RecyclerView) layout.findViewById(R.id.pop_recyclerview);

        final ArrayList<String> strings = new ArrayList<>();
        if (s.equals("1")){
            strings.clear();
            strings.add("灌气");
            strings.add("换气");
        }else if (s.equals("2")){
            strings.clear();
            strings.add("5KG");
            strings.add("10KG");
            strings.add("15KG");
            strings.add("20KG");
            strings.add("25KG");
        }
        PopAdapter popAdapter = new PopAdapter(this, strings);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        popRV.setLayoutManager(linearLayoutManager);
        popRV.setAdapter(popAdapter);

        popAdapter.setOnItemClickListener(new PopAdapter.OnItemClickListener() {
            @Override
            public void onClick(View view, PopAdapter.ViewName viewName, int position) {
                popupWindow.dismiss();
                switch (viewName){
                    case ITEM:
                        if (s.equals("1")){
                            products.get(i).setPro_name(strings.get(position));
                        }else if (s.equals("2")){
                            products.get(i).setWeight(strings.get(position));
                        }

                        ggAdapter.notifyDataSetChanged();
                        break;

                }
            }
        });

        popupWindow = new PopupWindow(layout, view.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT);
        //点击空白处时，隐藏掉pop窗口
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        //添加弹出、弹入的动画
        popupWindow.setAnimationStyle(R.style.Popupindow);
        popupWindow.showAsDropDown(view);
        popupWindow.showAtLocation(view, Gravity.TOP | Gravity.LEFT, location[0], location[1]+view.getHeight());
    }



    @Override
    public void onClick(View v) {
        list.add("");
        Product product = new Product();
        product.setIs_buy_new("0");
        product.setIs_change_new("0");
        product.setIs_annual_inspection("0");
        products.add(product);
        ggAdapter.notifyDataSetChanged();
    }
}
